<?php
namespace Home\Controller;
use Think\Controller;

class CartController extends CommonController {
	public function  _initialize(){
		parent::_initialize();
		$this->cartSrv = D('CartTmp');
		$this->commoditySrv = D('CommodityInfo');
		$this->commoditySrv->ini();
    }
	
    public function index() {
    	$this->display();
    }

	/**
	 * 添加购物袋
	 * @param string cart_info 商品信息的json字符串 
	 * {"commodity_id":"CM100111","commodity_num":2,"commodity_type":"common"}
	 * @return json
	 * @access public
	 *
	 */
	public function ajaxAddCart(){
		$result = array();
		$add_cart_info = I('cart_info',null);
		$add_cart_info = htmlspecialchars_decode($add_cart_info);
		// echo($add_cart_info);exit();
		// $add_cart_info = '{"commodity_id":"CM100112","commodity_num":2,"commodity_type":"common"}';
		if($this->checkLogin()==false){
			$response['msg'] = '尚未登陆';
			$response['toLogin'] = 1;
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$user_id = $this->uId();
		if(empty($add_cart_info)){
			$response['msg'] = '输入参数不能为空';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$add_cart_info = json_decode($add_cart_info,true);//将cart_info转成array
		if(empty($add_cart_info)){
			$response['msg'] = '商品格式不对';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		if(num_f($add_cart_info['commodity_num'])<=0){
			$response['msg'] = '该商品数量有误';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		if(empty($add_cart_info['commodity_type'])){
			$response['msg'] = '该商品类型有误';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$get_onsale = $this->commoditySrv->commodity_info($add_cart_info['commodity_id']);
		if(empty($get_onsale['is_onsale'])){
			$response['msg'] = '该商品已经下架';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		//验证库存
		$commodityLimit = D('CommodityLimit');
		$limit = $commodityLimit->getLimit($add_cart_info['commodity_id'],1);
		if($limit['is_limit']&&$limit['left_num']<$add_cart_info['commodity_num']){
			$response['msg'] = '加入失败，该商品还有'.$limit['left_num'].'份';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$cart_id = C('CART_PRE') . $user_id;//购物袋key
		//获取购物袋信息
		$cart_info_arr = json_decode($this->cartSrv->getCartTmp($cart_id),true);
		
		if(!empty($cart_info_arr)){
			$i=0;
			$is_added = false;
			foreach($cart_info_arr as $cart_info){
				if($add_cart_info['commodity_id'] == $cart_info['commodity_id']
					&& $add_cart_info['commodity_type'] == $cart_info['commodity_type']){
					$cart_info_arr[$i]['commodity_num'] = num_f($cart_info['commodity_num'])+num_f($add_cart_info['commodity_num']);
					$is_added = true;
				}
				$i++;
			}
			if(!$is_added){
				$cart_info_arr[] = $add_cart_info;
			}
		}else{
			$cart_info_arr[] = $add_cart_info;
		}
		$res_f = $this->cartSrv->setCartTmp($cart_id,yzg_json_encode($cart_info_arr));
		if($res_f){
			$response['msg'] = '添加购物袋成功';
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$this->ajaxReturn($response);
		}
	}
	
	/**
	 * 获取购物袋列表
	 * @return json
	 * @access public
	 *
	 */
	function ajaxGetCartList(){
		if($this->checkLogin()==false){
			$response['msg'] = '尚未登陆';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$user_id = $this->uId();
		$result = $this->cartSrv->getCartList($user_id);
		if($result['isok']==false){
			// $response['data'] = $result['data'];
			$response['msg'] = $result['msg'];
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		//还要获取商品信息
		$commInfo = array();
		foreach ($result['data']['list'] as $v) {
			$commInfo[$v['commodity_id']] = $this->commoditySrv->commodity_info($v['commodity_id']);
		}
		$result['data']['commInfo'] = $commInfo;
		$response['data'] = $result['data'];
		$response['msg'] = '获取成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	//获取购物车商品数量
	function ajaxGetCartNum(){
		if($this->checkLogin()==false){
			$response['num'] = 0;
			$response['msg'] = '尚未登陆';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$user_id = $this->uId();
		$num = 0;
		$cart_id = C('CART_PRE') . $user_id;//购物袋key
		//获取购物袋信息
		$cart_list_json = $this->cartSrv->getCartTmp($cart_id);
		if(empty($cart_list_json)){
			$response['num'] = 0;
			$response['msg'] = '空';
			$response['rsc'] = C('RESPONSE.SUCCESS');
			$this->ajaxReturn($response);
		}
		$cart_info_arr = json_decode($cart_list_json,true);
		if(!empty($cart_info_arr)){
			foreach($cart_info_arr as $cart_info){
				$num += num_f($cart_info['commodity_num']);
			}
		}
		$response['num'] = $num;
		$response['msg'] = '获取成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	public function ajaxUpdateCartNum(){
		if($this->checkLogin()==false){
			$response['msg'] = '尚未登陆';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$user_id = $this->uId();
		$commodity_id = I('commodity_id',null);
		$commodity_num = I('commodity_num',null);
		$commodity_type = I('commodity_type',null);
		if(empty($commodity_id) || empty($commodity_num) || empty($commodity_type)){
			$response['msg'] = '参数异常';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$cart_id = C('CART_PRE') . $user_id;//购物袋key
		$commodity_info = $this->commoditySrv->commodity_info($commodity_id);
		if(empty($commodity_info['is_onsale'])){
			$response['msg'] = '该商品已经下架';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		//验证库存
		$commodityLimit = D('CommodityLimit');
		$limit = $commodityLimit->getLimit($commodity_id,1);
		if($limit['is_limit']&&$limit['left_num']<$commodity_num){
			$response['msg'] = '修改失败，该商品还有'.$limit['left_num'].'份';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		
		//获取购物袋信息
		$cart_info_arr = json_decode($this->cartSrv->getCartTmp($cart_id),true);
		if(!empty($cart_info_arr)){
			$i=0;
			foreach($cart_info_arr as $cart_info){
				if($cart_info['commodity_id']==$commodity_id && $cart_info['commodity_type']==$commodity_type){
					$cart_info_arr[$i]['commodity_num'] = $commodity_num;
				}
				$i++;
			}
			$res_f = $this->cartSrv->setCartTmp($cart_id,yzg_json_encode($cart_info_arr));
			if(!$res_f){
				$response['msg'] = 'save操作失败';
				$response['rsc'] = C('RESPONSE.FAILED');
				$this->ajaxReturn($response);
			}
		}else{
			$response['msg'] = '获取购物袋信息失败';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$response['msg'] = '修改数量成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	/**
	 * 删除购物袋行记录
	 * @param string commodity_id
	 * @param string commodity_type
	 * @return json
	 * @access public
	 *
	 */
	public function ajaxDeleteCartOne(){
		if($this->checkLogin()==false){
			$response['msg'] = '尚未登陆';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$user_id = $this->uId();
		$commodity_id = I('commodity_id',null);
		$commodity_type = I('commodity_type',null);
		if(empty($commodity_id) || empty($commodity_type)){
			$response['msg'] = '参数异常';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$cart_id = C('CART_PRE') . $user_id;//购物袋key
		//获取购物袋信息
		$cart_info_arr = json_decode($this->cartSrv->getCartTmp($cart_id),true);
		if(!empty($cart_info_arr)){
			$i=0;
			foreach($cart_info_arr as $cart_info){
				if($cart_info['commodity_id']==$commodity_id && $cart_info['commodity_type']==$commodity_type){
					unset($cart_info_arr[$i]);
				}
				$i++;
			}
			sort($cart_info_arr);
			$res_f = $this->cartSrv->setCartTmp($cart_id,yzg_json_encode($cart_info_arr));
			if(!$res_f){
				$response['msg'] = 'save操作失败';
				$response['rsc'] = C('RESPONSE.FAILED');
				$this->ajaxReturn($response);
			}
		}else{
			$response['msg'] = '获取购物袋信息失败';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$response['msg'] = '删除记录成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	/**
	 * 删除购物袋行记录
	 * @param string commodity_id
	 * @param string commodity_type
	 * @return json
	 * @access public
	 *
	 */
	public function ajaxDeleteCartAll(){
		if($this->checkLogin()==false){
			$response['msg'] = '尚未登陆';
			$response['rsc'] = C('RESPONSE.FAILED');
			$this->ajaxReturn($response);
		}
		$user_id = $this->uId();
		$this->cartSrv->deleteCartAll($user_id);
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
};